/**
  CNOK project, Anyang Normal University, IMP-CAS
  \class TANuMom
  \brief calculate momentum distribution of the residue of direct 1-N removal
  This is a driver class of TARsMom, and hence a derived class from TANucleus
  \author SUN Yazhou, asia.rabbit@163.com
  \since 2022/06/22
  \date Last modified: 2023/02/05 by SUN Yazhou
  \copyright 2020-2023 SUN Yazhou
  \copyright CNOK project, Anyang Normal University, IMP-CAS
*/

#include "TANuMom.h"
#include "TARsMom.h"
#include "TAMOMDIS.h"
#include "TAParaManager.h"
#include "TAException.h"

TANuMom::TANuMom(int a, bool isconv) : TANucleus(a, 0., 0., 0., 0.), fIsConv(isconv){}

TANuMom::TANuMom(const string &f, bool isconv) : TANucleus(f), fIsConv(isconv){}

// calcualte Mom
void TANuMom::Mom(){
  const int n = fOrbit.size(), n1 = fEx.size(), n2 = fEx.size();
  if(n != n1 || n != n2) TAException::Error("TANuMom", "Mom: inconsistent vector sizes!");

  PARA->SetBaseDir(fBaseDir);
  // PARA->Clear(); // clean the previous text output of the base directory
  TARsMom rs(fA); // the momdis calculator
  for(int i = 0; i < n; i++){ // loop over all the valence configurations
    // set global parameters   fFNNParOpt: 0-> Horiuchi; 1-> LenziRay //
    if(0 == fFNNParOpt || 1 == fFNNParOpt) PARA->SetValue("FNNParOpt", fFNNParOpt);
    if(-9999. != fEk) PARA->SetValue("Ek", fEk); // MeV/u
    if(0 == fPauli || 1 == fPauli) PARA->SetValue("isPauli", bool(fPauli)); // 0: off; 1: on
    // do the calculation //
    char cfg[128]{}; rs.YamlName(fOrbit[i], cfg); // build the file name of the yaml
    TAMOMDIS mom(cfg, fIsConv);
    mom.Parallel(); // calculate the momdis
    int nm; double *kz; // kz[nm], to store the momdis
    if(mom.IsConv()){
      nm = mom.nmomconv(); // length of the convolved momentum array in mom
      kz = new double[nm]; for(int i = 0; i < nm; i++) kz[i] = mom.kzconv(i);
    } // end if
    else{
      nm = mom.nmom();
      kz = new double[nm]; for(int i = 0; i < nm; i++) kz[i] = mom.kz(i);
    } // end else
    rs.AssignKz(nm, kz); delete [] kz; kz = nullptr;
    // register the configuration
    if(mom.IsConv())
      rs.AddConfig(fOrbit[i], fEx[i], fC2S[i], mom.momstrConv().data(), mom.momtotConv().data());
    else
      rs.AddConfig(fOrbit[i], fEx[i], fC2S[i], mom.momstr().data(), mom.momtot().data());
  } // end for over i
  rs.Mom(); // calculate the inclusive momentum distribution
} // end member function Mom
